December 21, 2020
type
키워드를 사용해 새로운 타입 조합을 만들 수 있다.Union
을 많이 사용한다. type CustomType1 = string;
type CustomType2 = string | number | boolean;
type CustomType3 = {
name : string,
age : number,
isValid : boolean
} | [string, number, boolean];
let user1 : CustomType3 = {
name : 'sangmin',
age : 26,
isValid : true
};
let user2 : CustomType3 = ['sangmin', 26, true];
function func(val : CustomType1) : CustomType2 {
switch(val){
case 'string' : return val.toUpperCase();
case 'number' : return parseInt(val);
default : return true;
}
}
Union
인 타입들이 여러번 사용될 때, 특정한 변수에 담아서 사용하는 느낌?const
,let
과 비슷한 듯 하다.